/*
 * Created on 2005/1/25
 *
 */
package com.erp.framework.controller.taglib;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.taglib.html.SelectTag;

import com.erp.framework.util.logger.Logger;

public class CustomCollectionOptionsTag extends TagSupport {
    private String name;
    private String collection;
    private String statusLabel;
    private String filterStatus;
    private String selected;
    private String property;
    private String labelProperty;

    public String getLabelProperty() {
        return labelProperty;
    }

    public void setLabelProperty(String labelProperty) {
        this.labelProperty = labelProperty;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCollection() {
        return collection;
    }

    public void setCollection(String collection) {
        this.collection = collection;
    }

    public String getFilterStatus() {
        return filterStatus;
    }

    public void setFilterStatus(String filterStatus) {
        this.filterStatus = filterStatus;
    }

    public String getSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public String getStatusLabel() {
        return statusLabel;
    }

    public void setStatusLabel(String statusLabel) {
        this.statusLabel = statusLabel;
    }

    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        HttpSession session = pageContext.getSession();
        Iterator it = null;
        Object element = null;
        String label;
        String value;
        String status;
        StringBuffer sb = null;

        SelectTag parent = (SelectTag) TagSupport.findAncestorWithClass(this, SelectTag.class);

        try {
            Object bean = session.getAttribute(name);
            if (bean == null)
                throw new JspTagException("ErpCollectionOptions can't fine bean element under the label 'name'.");

            if (!(bean instanceof Collection)) {
                Collection collect = (Collection) PropertyUtils.getSimpleProperty(bean, collection);
                it = collect.iterator();
            } else
                it = ((Collection) bean).iterator();

            while (it.hasNext()) {
                element = it.next();
                value = (String) PropertyUtils.getSimpleProperty(element, property);
                label = (String) PropertyUtils.getSimpleProperty(element, labelProperty);
                status = (String) PropertyUtils.getSimpleProperty(element, statusLabel);

                if (value.equals(selected) || (parent != null && parent.isMatched(value)) || filterStatus == null
                        || !filterStatus.equals(status)) {
                    sb = new StringBuffer();
                    sb.append("<option value='");
                    sb.append(value);
                    sb.append("'");
                    if ((selected != null && value.equals(selected)) || (parent != null && parent.isMatched(value)))
                        sb.append(" selected");
                    sb.append(">");
                    sb.append(label);
                    sb.append("</option>");
                    out.println(sb.toString());
                }
            }
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }

        return EVAL_PAGE;
    }

    public void release() {
        super.release();
        name = null;
        collection = null;
        statusLabel = null;
        filterStatus = null;
        selected = null;
        property = null;
        labelProperty = null;
    }

}
